package com.changgo.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgo.service.PageService;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Value("${pagepath}")
    private String pagepath;
    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void generateItemPage(String spuId) {
        //获取context对象,用于存放商品详情数据
        Context context = new Context();
        Map<String, Object> itemData = findItemData(spuId);
        context.setVariables(itemData);

        File file = new File(pagepath);
        if (!file.exists()) {
            file.mkdirs();
        }
        File dest = new File(file + "/" + spuId + ".html");
        Writer out = null;
        try {
            out = new PrintWriter(dest);
            templateEngine.process("item", context, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private Map<String, Object> findItemData(String spuId) {
        //调用商品微服务,查询商品详情数据
        Map<String, Object> hashMap = new HashMap<>();
        //远程调用获取spu
        Spu spu = spuFeign.findSpuById(spuId).getData();
        hashMap.put("spu", spu);
        //获取图片信息
        if (spu != null) {
            if (StringUtils.isNotEmpty(spu.getImages())) {
                hashMap.put("imageList", spu.getImages().split(","));
            }
        }
        //远程调用获取商品分类
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        hashMap.put("category1", category1);
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        hashMap.put("category2", category2);
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();
        hashMap.put("category3", category3);
        //远程调用获取sku
        //获取sku集合信息
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        hashMap.put("skuList", skuList);
        hashMap.put("specificationList", JSON.parseObject(spu.getSpecItems(), Map.class));
        return hashMap;
    }
}

